const { result, add } = require("lodash");
const db = require("../db");
const { query } = require("express-validator");
// 获取文章分类
exports.getCateList = (req, res) => {
  const sql =
    "select * from ev_article_cate where is_delete = 0 order by id asc";
  db.query(sql, (err, results) => {
    if (err) return res.cc(err.message);
    res.send({
      status: 200,
      message: "获取文章分类数据成功",
      data: results,
    });
  });
};

// 新增文章分类
exports.addArticleCate = (req, res) => {
  // res.send('新增文章分类 ok')
  // 判断分类名称与分类别名是否被占用
  const sql = "select * from ev_article_cate where name = ? or alias = ?";
  db.query(sql, [req.body.name, req.body.alias], (err, results) => {
    if (err) return res.cc(err.message);
    // 检查重复项
    const nameExists = results.some((item) => item.name === req.body.name);
    const aliasExists = results.some((item) => item.alias === req.body.alias);

    if (nameExists && aliasExists) {
      return res.cc("分类名称与分类别名都已存在");
    } else if (nameExists) {
      return res.cc("分类名称已存在");
    } else if (aliasExists) {
      return res.cc("分类别名已存在");
    }
    // 增加文章分类
    const addSql = "insert into ev_article_cate set ?";
    db.query(
      addSql,
      { name: req.body.name, alias: req.body.alias },
      (err, results) => {
        if (err) return res.cc(err.message);
        if (results.affectedRows !== 1) return res.cc("新增文章分类失败");
        res.cc("新增文章分类成功", 200);
      }
    );
  });
};

// 删除文章分类
exports.deleteArticle = (req, res) => {
  const id = req.params.id;
  //  检查id在不在
  const checkSql = "select * from ev_article_cate where id = ?";
  db.query(checkSql, id, (err, results) => {
    if (err) return res.cc(err.message);
    if (results.length === 0) return res.cc("分类id不存在");
    // 执行删除操作
    const updateSql = "update ev_article_cate set is_delete=1 where id = ?";
    db.query(updateSql, req.params.id, (err, results) => {
      if (err) return res.cc(err.message);
      if (results.affectedRows !== 1) return res.cc("删除失败");
      res.cc("删除成功", 200);
    });
  });
};

// 根据id查询文章分类数据
exports.getCateListById = (req, res) => {
  // res.send('idhuoqu')
  const sql = "select * from ev_article_cate where id = ?";
  db.query(sql, req.params.id, (err, results) => {
    if (err) return res.cc(err.message);
    if (results.length !== 1) return res.cc("查询失败");
    res.send({
      status: 200,
      message: "查询成功",
      data: results[0],
    });
  });
};

// 根据id更新分类文章数据
exports.updateCateById = (req, res) => {
  const { id,name, alias } = req.body;
  
  // 1. 验证ID是否存在
  const checkIdSql = "SELECT * FROM ev_article_cate WHERE id = ? AND is_delete = 0";
  db.query(checkIdSql, [id], (err, idResults) => {
    if (err) return res.cc(err.message);
    if (idResults.length === 0) return res.cc("分类ID不存在或已被删除");
    
    // 2. 检查更新的name和alias是否与其他分类重复（排除当前分类）
    const checkDuplicateSql = `
      SELECT * FROM ev_article_cate 
      WHERE (name = ? OR alias = ?) 
        AND id <> ? 
        AND is_delete = 0
    `;
    
    db.query(checkDuplicateSql, [name, alias, id], (err, dupResults) => {
      if (err) return res.cc(err.message);
      
      // 检查重复项
      const nameExists = dupResults.some(item => item.name === name);
      const aliasExists = dupResults.some(item => item.alias === alias);
      
      if (nameExists && aliasExists) {
        return res.cc("分类名称与分类别名都已存在");
      } else if (nameExists) {
        return res.cc("分类名称已存在");
      } else if (aliasExists) {
        return res.cc("分类别名已存在");
      }
      
      // 3. 执行更新操作
      const updateSql = `
        UPDATE ev_article_cate 
        SET name = ?, alias = ?
        WHERE id = ? AND is_delete = 0
      `;
      
      const updateData = [name, alias, id];
      
      db.query(updateSql, updateData, (err, updateResults) => {
        if (err) return res.cc(err.message);
        
        if (updateResults.affectedRows !== 1) {
          return res.cc("更新分类失败");
        }
        
        res.cc("更新分类成功", 200);
      });
    });
  });
};
